<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!--
        保证校验生效的三要素:
          1. model
          2. rules
          3. prop
       -->
      <el-form :model="regForm" :rules="regRules" ref="regForm">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="regForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" type="password" v-model="regForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input placeholder="请再次输入密码" type="password" v-model="regForm.repassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="reg" style="width: 100%" type="primary">注册</el-button>
          <el-link @click="$router.push('/login')" type="info">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Reg',
  data () {
    const checkPwd = (rule, value, callback) => {
      if (this.regForm.repassword !== this.regForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      regRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z][0-9a-zA-Z]{0,9}$/, message: '用户名必须是 1 ~ 10 位的字母数字, 不能以数字开头', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是 6 ~ 15 位非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是 6 ~ 15 位非空字符', trigger: 'blur' },
          { validator: checkPwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg () {
      // 1. 兜底校验 手动调用校验方法, 确保用户全部输入正确后才发请求
      // async 必须写在 await 所在的函数声明前
      this.$refs.regForm.validate(async valid => {
        if (!valid) return
        // 2. 发请求
        // console.log('发请求注册')
        // const res = await this.$http.post('/api/reg', {
        //   username: this.regForm.username,
        //   password: this.regForm.password,
        //   repassword: this.regForm.repassword
        // })
        const res = await this.$http.post('/api/reg', this.regForm)
        // console.log(res.data)
        // 3. 获取到结果后应该对结果进行判断, 提醒用户
        if (res.data.code !== 0) return this.$message.error(res.data.message)
        // 能走下来说明成功了
        // console.log(res.data)
        // 注册成功后提醒用户并跳转至登录页
        this.$message.success(res.data.message)
        // 4. 编程式导航跳转至登录页
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    padding: 0 30px;

    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
  }
}
</style>
